<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>getViewSize</title>
</head>

<body>
    <div id="test"></div>
    <script>
        const getViewSize = () => {
            if (window.innerWidth) {
                return {
                    width: window.innerWidth,
                    height: window.innerHeight
                }
            } else if (document.compatMode === 'CSS1Compat') {
                return {
                    width: document.documentElement.clientWidth,
                    height: document.documentElement.clientHeight
                }
            } else {
                return {
                    width: document.body.clientWidth,
                    height: document.body.clientHeight
                }
            }
        }
        const handler = () => {
            const { width, height } = getViewSize();
            document.querySelector('#test').innerHTML = `当前窗口宽度是:${width},高度是:${height}。`
        }
        window.addEventListener('resize', handler);
        handler();
    </script>
</body>

</html>